//
//  FindViewController.m
//  Project_A
//
//  Created by 房磊 on 15/1/15.
//  Copyright (c) 2015年 Flowers. All rights reserved.
//

#import "FindViewController.h"
#import "AFNetworking.h"
#import "MJRefresh.h"

#define BUTTON_Y 74
#define BUTTON_X 10
#define BUTTON_WIDTH 60
#define BUTTON_HEIGHT 30
@interface FindViewController ()

@end

@implementation FindViewController
- (void)dealloc
{
    [_tableView release];
    [_array release];
    [_dataArray release];
    [_type release];
    [_playBut release];
    [_likeBut release];
    [_downVBut release];
    [_commentBut release];
    [super dealloc];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor purpleColor];
    self.navigationItem.title = @"发现";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 104, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.index = 1;
    //布页面
    [self creatSubView];

    //调用上下拉刷新的方法
    [self setUpRefresh];
    
    
}
//cell的重用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentfieir = @"resue";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentfieir];
    if (cell == nil) {
        
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentfieir];
    }
    FindModel *model = [self.dataArray objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    detailViewController *detailView = [[[detailViewController alloc] init] autorelease];
    FindModel *model = [self.dataArray objectAtIndex:indexPath.row];
    //属性传值
    detailView.name = model.albumName;
    detailView.playNam = model.albumPlayNum;
    detailView.albumId = model.albumId;
    detailView.introduce = model.albumIntroduction;
    detailView.Pic = model.albumPic;
    [self.navigationController pushViewController:detailView animated:YES];
    
}
//触发button时 ,先判断是哪一个button,将不同的参数传给接口,去解析数据
- (void)actionButton:(UIButton *)button
{
// AFNetworkReachabilityManager *netWorkManager =  [AFNetworkReachabilityManager sharedManager];
    
    NSInteger i;
    if (button == self.playBut) {
        i = 0;
    }else if (button == self.downVBut)
    {
        i = 1;
    }else if (button == self.likeBut)
    {
        i = 2;
    }else
    {
        i = 3;
    }
    self.type = [NSString stringWithFormat:@"%ld",i];
    //数据请求
    [self AFN];
}
//用MJ完成刷新
- (void)setUpRefresh
{
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
}
//下拉刷新
- (void)headerRefresh
{
    
    self.index--;
    [self AFN];
    [self.tableView reloadData];
    [self.tableView headerEndRefreshing];
}
//上拉刷新
- (void)footerRefresh
{
    self.index++;
    [self AFN];
    [self.tableView reloadData];
    [self.tableView footerEndRefreshing];

}
//网络数据请求
- (void)AFN
{
    //刷新数据时拼接的参数,每页20条
    NSString *str_page = [NSString stringWithFormat:@"%ld",self.index];
    
    NSString *url_str =[NSString stringWithFormat:@"http://www.thatbbs.com/newAudio/api/audioAlbumList.do"];
    //POST请求需要的body体
    NSDictionary *dic = @{@"page":str_page, @"version":@"1.0", @"type":self.type};
    AFHTTPRequestOperationManager *manager =
    [AFHTTPRequestOperationManager manager];
    //不同的接口文档,用到的不一样text/plain  text/html ...
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    //在block中最好不要直接用属性赋值
    __block FindViewController *own = self;
    [manager POST:url_str parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject){
        NSDictionary *dic = [responseObject objectForKey:@"response"];
        NSMutableArray *array = dic[@"ReturnData"];
        self.dataArray = [NSMutableArray array];
        //取出数据 给model属性
        for (NSDictionary *dataDic in array) {
            FindModel *model = [[FindModel alloc] init];
            model.albumName = dataDic[@"albumName"];
            model.albumPlayNum =[NSString stringWithFormat:@"%@",dataDic[@"albumPlayNum"]];
            model.albumBigPic = dataDic[@"albumBigPic"];
            model.albumPic = dataDic[@"albumPic"];
            model.albumId = [NSString stringWithFormat:@"%@", dataDic[@"albumId"]];
            model.albumIntroduction = dataDic[@"albumIntroduction"];
            
            [own.dataArray addObject:model];
            
        }
        //刷新tableView
        [own.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败");
    }];

}
//添加按钮
- (void)creatSubView
{
    UIButton *categoryBut = [UIButton buttonWithType:UIButtonTypeCustom];
    categoryBut.frame = CGRectMake(BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
    [categoryBut setTitle:@"分类" forState:UIControlStateNormal];
//    [categoryBut addTarget:self action:@selector(categoryButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:categoryBut];
    
    self.playBut = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playBut.frame = CGRectMake(categoryBut.frame.origin.x + BUTTON_WIDTH + BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
    [self.playBut setTitle:@"播放榜" forState:UIControlStateNormal];
    [self.playBut addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.playBut];
    self.downVBut = [UIButton buttonWithType:UIButtonTypeCustom];
    self.downVBut.frame = CGRectMake(self.playBut.frame.origin.x + BUTTON_WIDTH + BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
    [self.downVBut setTitle:@"下载榜" forState:UIControlStateNormal];
    
    [self.downVBut addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.downVBut];
    self.likeBut = [UIButton buttonWithType:UIButtonTypeCustom];
    self.likeBut.frame = CGRectMake(self.downVBut.frame.origin.x + BUTTON_WIDTH + BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
    [self.likeBut setTitle:@"喜爱榜" forState:UIControlStateNormal];
    [self.likeBut addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.likeBut];
    self.commentBut = [UIButton buttonWithType:UIButtonTypeCustom];
    self.commentBut.frame = CGRectMake(self.likeBut.frame.origin.x + BUTTON_WIDTH +BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
    [self.commentBut setTitle:@"评论榜" forState:UIControlStateNormal];
    [self.commentBut addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.commentBut];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
